package com.smarth.zhizulian.app;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.multidex.MultiDex;
import android.widget.Toast;

import com.smarth.zhizulian.activity.AgentWebActivity;
import com.smarth.zhizulian.activity.LoginActivity;
import com.smarth.zhizulian.entity.DaoMaster;
import com.smarth.zhizulian.entity.DaoSession;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.entity.UmengJson;
import com.smarth.zhizulian.http.utils.NetConnectUtils;
import com.smarth.zhizulian.utils.GsonUtils;
import com.smarth.zhizulian.utils.KumaLog;
import com.smarth.zhizulian.utils.SPUtils;
import com.smarth.zhizulian.utils.UserUtils;
import com.mob.MobApplication;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

/**
 * Created by 18865 on 2017/12/23.
 */

public class BaseApplication extends MobApplication {
    private Context mContext;
    private static DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        SPUtils.init(mContext);
        EventBus.getDefault().register(this);
        NetConnectUtils.init(mContext);
        Bugly.init(getApplicationContext(), "d1f13b69e3", BaseConfig.IS_DEBUG);
        initPush();
        setupDatabase();
    }

    private void initPush() {
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "f2b919959e997b9cd78a3057e9737d17");
        PushAgent mPushAgent = PushAgent.getInstance(this);
//注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
                KumaLog.kuma("deviceToken:" + deviceToken);
            }

            @Override
            public void onFailure(String s, String s1) {
                KumaLog.kuma("s:" + s + ", s1:" + s1);
            }
        });
        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage uMessage) {
                String json = uMessage.custom;
                KumaLog.kuma(json);
                UmengJson uj = GsonUtils.fromJson(json, UmengJson.class);
                //打开网页
                if ("0".equals(uj.getType())) {
                    Intent i = new Intent(context, AgentWebActivity.class);
                    i.putExtra(AgentWebActivity.INTENT_URL, injectParams(uj.getUrl()));
                    i.putExtra(AgentWebActivity.INTENT_TITLE, "网页详情");
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                }
            }
        };
        mPushAgent.setNotificationClickHandler(notificationClickHandler);
    }

    @Subscriber(tag = BaseConfig.EventTag.RELOGIN)
    private void relogin(EventBean eb) {
        LoginActivity.openForReLogin(mContext);
    }

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public static String injectParams(String url) {
        if (url != null && !url.contains("maicaitoken=") && UserUtils.isLogin()) {
            if (url.contains("?")) {
                return url + "&maicaitoken=" + UserUtils.getToken();
            } else {
                return url + "?maicaitoken=" + UserUtils.getToken();
            }
        } else {
            return url;
        }
    }

    /**
     * 配置数据库
     */
    private void setupDatabase() {
        //创建数据库shop.db"
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "baozhuan.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoInstant() {
        return daoSession;
    }
}
